package com.headStorm.guava;

import com.google.common.base.Joiner;
import com.google.common.collect.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Example1 {

    public static void main(String[] args) throws InterruptedException {
        //handString();
        //collectionUse();
        List<Integer> aa = new ArrayList<>();
        aa.add(1);
        aa.add(2);
        aa.add(3);



        int num = 0;
        for (int i = 0; i < aa.size() - 1; i++) {
            num += aa.get(i+1) - aa.get(i);
        }
        //System.out.println(num);
        System.out.println(num);
    }

    /**
     * 字符处理相关
     */
    public static void handString(){
        List<String> words = Lists.newArrayList("123","456","789",null);
        //不使用guava
        StringBuilder sb = new StringBuilder();
        for(String word : words){
            if(word==null){
                sb.append("default");
            }else {
                sb.append(word);
            }
            sb.append(",");
        }
        if(sb.length()>1){
            sb.deleteCharAt(sb.length()-1);
        }
        System.out.println(sb.toString());

        //使用guava
        System.out.println(Joiner.on(",").useForNull("default").join(words));
        System.out.println(Joiner.on(",").skipNulls().join(words));


        Map<String, String> data = ImmutableMap.of("a", "1", "b", "2");
        System.out.println(Joiner.on(",").withKeyValueSeparator("-").join(data));
        //output:a-1,b-2
        Map<String, Integer> data2 = ImmutableMap.of("a", 1, "b", 2);
        System.out.println(Joiner.on(",").withKeyValueSeparator("-").join(data2));
        //output:a-1,b-2
    }

    public static void collectionUse(){
        List<String> words = Lists.newArrayList("a","b","c","b","b","c");

        Multiset<String> multiset1 = HashMultiset.create();
        for(String word : words){
            multiset1.add(word);
        }
        System.out.println(multiset1);


        //判断每个元素出现的次数
        for (String key : multiset1.elementSet()) {
            System.out.println(multiset1.count(key));
        }

        Multiset<String> multiset2 = HashMultiset.create(words);
        multiset2.add("d",4);
        System.out.println(multiset2);
    }

    /**
     * guava collection工具类的使用
     * @throws InterruptedException
     */
    public static void  colletionUtil() throws InterruptedException {
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
        List<String> buffer = new ArrayList<>();

        queue.put("aaa");
        queue.put("bbb");
        queue.put("ccc");
        queue.put("dddd");
        queue.put("eeee");

        //当队列中的数据条数达到一定条数，则会执行下面代码，或是等待时间大于五秒时候,否则该线程一直阻塞。
        Queues.drain(queue, buffer, 3, 5, TimeUnit.SECONDS);
        System.out.println("------");
        System.out.println(queue.size());
        System.out.println(buffer.size());

        List<String> data = new ArrayList<>();
        data.add("aa");
        data.add("bb");
        data.add("cc");

        List<String> reverseData = Lists.reverse(data);
        System.out.println(reverseData);
    }


}
